package com.ujiuye.dao;

import com.ujiuye.bean.User;
import com.ujiuye.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDao {
    private QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
    public User getUserByNameAndPassword(String username, String pwd){
        String sql="select * from user where username=? and password=?";
        User user=null;
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), username, pwd);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables.getMessage());
        }
        return user;
    }
    public List<User> selectAllUser(){
        String sql="select * from user";
        List<User> uli=null;
        try {
            uli = queryRunner.query(sql, new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return uli;
    }
}
